package shixun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import shixun.domain.accountInf;
import shixun.domain.User;
import shixun.service.accountInfService;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AccountInfController {

    @Autowired
    private accountInfService accountInfService;


    @RequestMapping("jumpTransferOthers")
    public String jumpTransferOthers() {
        return "transferOthers";
    }


    @RequestMapping("/jumpDeposit")
    public String jumpDeposit() {
        return "deposit";
    }

    @RequestMapping("/jumpDepositCredit")
    public String jumpDepositCredit() {
        return "depositCredit";
    }

    @RequestMapping("/jumpWithdrawals")
    public String jumpWithdrawals() {
        return "withdrawals";
    }

    @RequestMapping("/jumpWithdrawalsCredit")
    public String jumpWithdrawalsCredit() {
        return "withdrawalsCredit";
    }


    @RequestMapping("/deposit")
    public String deposit(double money, HttpSession session, int type) {
        User user = (User) session.getAttribute("loginUser");
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        double currentMoney = accountInfService.getCurrentMoney(user.getId(), type);
        if (currentMoney == -0.001) {
            return "NoSave";
        }else{
            double AllMoney = currentMoney + money;
            String depositInformation = format + "存入" + money + "元";
            accountInfService.insertInformation(depositInformation, user.getId(), AllMoney, type);
            return "redirect:/index";
        }
    }

    @RequestMapping("/depositCredit")
    public String depositCredit(double money, HttpSession session, int type) {
        User user = (User) session.getAttribute("loginUser");
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        double currentMoney = accountInfService.getCurrentMoney(user.getId(), type);
        if(currentMoney==-0.001){
            return "NoCredit";
        }else{
            double AllMoney = currentMoney + money;
            String depositInformation = format + "存入" + money + "元";
            accountInfService.insertInformation(depositInformation, user.getId(), AllMoney, type);
            return "redirect:/index";
        }
    }


    @RequestMapping("/withdrawals")
    public String withdrawals(double money, HttpSession session, int type) {
        User user = (User) session.getAttribute("loginUser");
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        double currentMoney = accountInfService.getCurrentMoney(user.getId(), type);
        if (currentMoney == -0.001) {
            return "NoSave";
        }else {
            double AllMoney = currentMoney - money;
            if (AllMoney < 0) {
                return "error";
            } else {
                String withdrawalsInformation = format + "取出" + money + "元";
                accountInfService.insertInformation(withdrawalsInformation, user.getId(), AllMoney, type);
                return "redirect:/index";
            }
        }
    }

    @RequestMapping("/withdrawalsCredit")
    public String withdrawalsCredit(double money, HttpSession session, int type) {
        User user = (User) session.getAttribute("loginUser");
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        double currentMoney = accountInfService.getCurrentMoney(user.getId(), type);
        if (currentMoney==-0.001){
            return "NoCredit";
        }else{
            double AllMoney = currentMoney - money;
            String withdrawalsInformation = format + "消费" + money + "额度";
            accountInfService.insertInformation(withdrawalsInformation, user.getId(), AllMoney, type);
            return "redirect:/index";
        }
    }

    @RequestMapping("/zhuanzhang")
    public String zhuanzhang(double money, int id, HttpSession session, int type) {
        User user = (User) session.getAttribute("loginUser");
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        if (user.getId() == id) {
            return "error";
        }
        double currentOwnMoney = accountInfService.getCurrentMoney(user.getId(), type);
        double AllOwnMoney = currentOwnMoney - money;
        double currentAnotherMoney = accountInfService.getCurrentMoney(id, type);
        double AllAnotherMoney = currentAnotherMoney + money;
        if (AllOwnMoney < 0) {
            return "error";
        } else {
            String transferInformationOwn = format + "转给id为" + id + "的用户" + money + "元";
            String transferInformationAnother = format + "id为" + user.getId() + "的用户转入" + money + "元";
            accountInfService.insertInformation(transferInformationOwn, user.getId(), AllOwnMoney, type);
            accountInfService.insertInformation(transferInformationAnother, id, AllAnotherMoney, type);
            return "redirect:/index";
        }
    }

    @RequestMapping("/findInformation")
    public String findInformationByIdAndType(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        List<accountInf> savingsdepositcardinfList = accountInfService.findInformationById(user.getId(), 0);
        session.setAttribute("savingsdepositcardinfList", savingsdepositcardinfList);
        return "accountList";
    }

    @RequestMapping("/findInformationCredit")
    public String findInformationByIdAndTypeCredit(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        List<accountInf> savingsdepositcardinfList = accountInfService.findInformationById(user.getId(), 1);
        session.setAttribute("savingsdepositcardinfList", savingsdepositcardinfList);
        return "accountList";
    }

    @RequestMapping("/findOtherById")
    public String findOtherUser(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        List<User> OthersList = accountInfService.findOtherById(user.getId());
        session.setAttribute("OthersList", OthersList);
        return "transferOthers";
    }
}
